package com.rxyl.love.petdoctor.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.adapter.CircleItemAdapter;
import com.rxyl.love.petdoctor.app.MyApplication;
import com.rxyl.love.petdoctor.entity.CircleEntity;
import com.rxyl.love.petdoctor.util.NetworkUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zuchang on 2016/1/20.
 */
public class CircleDetailFragment extends Fragment{
    private CircleItemAdapter circleItemAdapter ;
    private List<CircleEntity.Items> datas = new ArrayList<>();
    private ListView listView;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.circle_frame,container,false);
      //      View view = inflater.inflate(R.layout.circle_frame,null);
        listView = (ListView) view.findViewById(R.id.lv_circle);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        loadDatas();
    }

    private void initView() {
        circleItemAdapter = new CircleItemAdapter(MyApplication.getApp(),datas);
        listView.setAdapter(circleItemAdapter);
    }


    private void loadDatas() {
        NetworkUtil.getInstance().getCircle(new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                Gson gson = new Gson();
                CircleEntity circle = gson.fromJson(responseInfo.result.toString(),CircleEntity.class);
                if (circle!=null){
                    datas.addAll(circle.getItems());

                }
                circleItemAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

}
